package com.gsi.webstore.platform.storefront.pipelet.orderprocess;

import java.util.Iterator;
import com.intershop.beehive.core.capi.pipeline.Pipelet;
import com.intershop.beehive.core.capi.pipeline.PipelineDictionary;
import com.intershop.beehive.core.capi.pipeline.PipeletExecutionException;
import com.intershop.beehive.bts.capi.orderprocess.LineItem;
import com.intershop.beehive.bts.capi.orderprocess.ProductLineItem;
import com.intershop.beehive.bts.capi.orderprocess.basket.Basket;
import com.gsi.webstore.platform.adapter.storelocator.internal.client.data.Storefront;

/**
 * 
 */
public class VerifyProductExistenceInCart extends Pipelet
{

    /**
     * Constant used to access the pipeline dictionary with key 'Cart'
     */
    public static final String DN_CART = "Cart";
    /**
     * Constant used to access the pipeline dictionary with key 'ProductSKU'
     */
    public static final String DN_PRODUCT_SKU = "ProductSKU";
    /**
     * Constant used to access the pipeline dictionary with key
     * 'ProductShippingMethod'
     */
    public static final String DN_PRODUCT_SHIPPING_METHOD = "ProductShippingMethod";
    /**
     * Constant used to access the pipeline dictionary with key 'ResultFlag'
     */
    public static final String DN_RESULT_FLAG = "ResultFlag";
    /**
     * Constant used to access the pipeline dictionary with key 'Result'
     */
    public static final String DN_RESULT = "Result";
    /**
     * Constant used to access the pipeline dictionary with key
     * 'ExistingLineItem'
     */
    public static final String DN_EXISTING_LINE_ITEM = "ExistingLineItem";
    /**
     * Constant used to access the pipeline dictionary with key
     * 'ProductShipToAddress'
     */
    public static final String DN_PRODUCT_SHIP_TO_ADDRESS = "ProductShipToAddress";
    /**
     * Constant used to access the pipeline dictionary with key
     * 'ProductShipToAddressID'
     */
    public static final String DN_PRODUCT_SHIP_TO_ADDRESS_ID = "ProductShipToAddressID";
    /**
     * Constant used to access the pipeline dictionary with key 'Storefront'
     */
    public static final String DN_STOREFRONT = "Storefront";
    String storeFrontIdAttribute = "STORE_ID";
    String storeCodeAttribute = "STORE_CODE";

    /**
     * The pipelet's execute method is called whenever the pipelets gets
     * executed in the context of a pipeline and a request. The pipeline
     * dictionary valid for the currently executing thread is provided as a
     * parameter.
     * 
     * @param dict
     *            The pipeline dictionary to be used.
     * @throws PipeletExecutionException
     *             Thrown in case of severe errors that make the pipelet execute
     *             impossible (e.g. missing required input data).
     */
    public int execute(PipelineDictionary dict) throws PipeletExecutionException
    {
        // lookup 'Storefront' in pipeline dictionary
        Storefront storefront = (Storefront)dict.get(DN_STOREFRONT);

        // lookup 'ProductShippingMethod' in pipeline dictionary
        String productShippingMethod = (String)dict.get(DN_PRODUCT_SHIPPING_METHOD);
        if (null == productShippingMethod)
        {
            productShippingMethod = "";
        }

        // lookup 'ProductSKU' in pipeline dictionary
        String productSKU = (String)dict.get(DN_PRODUCT_SKU);
        if (null == productSKU)
        {
            throw new PipeletExecutionException(
                            "Mandatory input parameter 'ProductSKU' not available in pipeline dictionary.");
        }

        // lookup 'Cart' in pipeline dictionary
        Basket cart = (Basket)dict.get(DN_CART);
        if (null == cart)
        {
            throw new PipeletExecutionException(
                            "Mandatory input parameter 'Cart' not available in pipeline dictionary.");

        }
        String pliShippingMethod = new String();
        String storeCode = null;
        Long storeId = null;
        boolean resultFlag = false;
        ProductLineItem pli = null;
        ProductLineItem existingLineItem = null;
        Iterator<ProductLineItem> pliIterator= cart.createProductLineItemsOrderedByPositionIterator();
        if (pliIterator != null)
        {
            while(pliIterator.hasNext())
            {
                pli = pliIterator.next();
                String pliProductSKU = pli.getOfferProductRef().getProductSKU().toString();
                storeCode = (String)pli.getAttribute(storeCodeAttribute);
                storeId = (Long)pli.getAttribute(storeFrontIdAttribute);
                if (pliProductSKU.equals(productSKU))
                {
                    if (pli.getShippingMethod() != null)
                    {
                        pliShippingMethod = pli.getShippingMethod().getId();
                        if (pliShippingMethod.equals(productShippingMethod))
                        {
                            if (storefront != null && storeCode !=null && storeId !=null )
                            {
                                if (storeCode.equals(storefront.getStoreCode()) && storeId.equals(storefront.getId()))
                                {
                                    existingLineItem = pli;
                                    resultFlag = true;
                                    break;
                                }
                            }
                            else
                            {
                                if (storefront == null && storeCode == null && storeId == null)
                                {
                                    existingLineItem = pli;
                                    resultFlag = true;
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (pliShippingMethod.equals(productShippingMethod))
                        {
                            if (storefront != null)
                            {
                                if (storeCode.equals(storefront.getStoreCode()) && storeId.equals(storefront.getId()))
                                {
                                    existingLineItem = pli;
                                    resultFlag = true;
                                    break;
                                }
                            }
                           
                        }

                    }

                }

            }

        }

        // store 'ResultFlag' in pipeline dictionary
        // TODO Put the value in dictionary
        dict.put(DN_RESULT_FLAG, resultFlag);

        // store 'ExistingLineItem' in pipeline dictionary
        // TODO Put the value in dictionary
        dict.put(DN_EXISTING_LINE_ITEM, existingLineItem);
        return PIPELET_NEXT;
    }
}